package com.winning.sjzc.old.controller;

import com.winning.sjzc.common.utils.*;
import com.winning.sjzc.old.service.ZyglService;
import com.winning.sjzc.zygl.entity.Zygl;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/*
 * @Author tangzj
 * @Description  值域管理 控制层
 * @Date 2019/5/10 8:50
 **/
//@RestController
//@RequestMapping("/zygl")
public class ZyglController {

    @Autowired
    private ZyglService zyglService;

    /*共同赋值*/
    private void CommonZy(Zygl zygl, Zygl zyglUpdate){
        zygl.setGlnd(zyglUpdate.getGlnd());
        zygl.setZydm(zyglUpdate.getZydm());
        zygl.setZymc(zyglUpdate.getZymc());
        zygl.setZyoid(zyglUpdate.getZyoid());
        zygl.setZylbsm(zyglUpdate.getZylbsm());
        zygl.setZysm(zyglUpdate.getZysm());
        this.zyglService.updateZyx(zygl);
    }

    /*通过参数获取值域管理的值域和值域项*/
    private Zygl getZygl(Map<String,Object> map){
        return this.zyglService.getZygl(map);
    }

    /*
     * @Author tangzj
     * @Description 目录查询
     * @Date 2019/5/10 8:50
     * @Param []
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value = "/queryZyTree")
    public ResponseEntity queryZyTree(){
        return ResponseEntity.ok().put("list",zyglService.queryZyTree());
    }

    /*
     * @Author tangzj
     * @Description 保存值域
     * @Date 2019/5/10 8:54
     * @Param []
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/saveZy",method = RequestMethod.POST)
    public ResponseEntity saveZy(@RequestBody Zygl zygl){
        if (!Common.validateParams(zygl.getZydm())) {
            return ResponseEntity.error(201, "参数为空");
        }
        //值域代码是否存在
        Map<String,Object> map =  new HashedMap<String,Object>();
        map.put("zydm",zygl.getZydm());
        map.put("zylbsm",zygl.getZylbsm());
        Long i =  zyglService.queryByZydm(map);
        if(i>0){
            return ResponseEntity.error(201, "值域代码已经存在");
        }
        Calendar calendar=Calendar.getInstance();
        zygl.setGlnd(calendar.get(Calendar.YEAR));
        this.zyglService.saveZy(zygl);
        ResponseEntity rp = new ResponseEntity();
        rp.put("zy",zygl);
        return  rp;
    }

    /*
     * @Author tangzj
     * @Description 获取值域项
     * @Date 2019/5/10 15:50
     * @Param [zdm]
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/getZy",method = RequestMethod.GET)
    public ResponseEntity getZy(@RequestParam String zydm){
        if (!Common.validateParams(zydm)) {
            return ResponseEntity.error(201, "参数为空");
        }
        Map<String,Object> map  = new HashedMap<String,Object>();
        map.put("zydm",zydm);
        Zygl zygl =  this.getZygl(map);
        ResponseEntity responseEntity = new ResponseEntity();
        return  responseEntity.put("zy",zygl);
    }

    /*
     * @Author tangzj
     * @Description 值域更新
     * @Date 2019/5/10 10:17
     * @Param [zygl]
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/updateZy",method = RequestMethod.POST)
    public ResponseEntity updateZy(@RequestBody Zygl zygl){
        this.zyglService.updateZy(zygl);
        ResponseEntity rp = new ResponseEntity();
        rp.put("zy",zygl);
        return  rp;
    }

    /*
     * @Author tangzj
     * @Description 删除值域
     * @Date 2019/5/10 13:35
     * @Param [zydm]
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/delZy",method = RequestMethod.GET)
    public ResponseEntity delZy(@RequestParam String zydm){
        if (!Common.validateParams(zydm)) {
            return ResponseEntity.error(201, "参数为空");
        }
        //先查询库中当前值代码下的值域还有几个，如果是一个的话，更新数据，否则删除
        Map<String,Object> map  = new HashedMap<String,Object>();
        map.put("zydm",zydm);
        this.zyglService.delete(map);
        return  ResponseEntity.ok();
    }

    /*
     * @Author tangzj
     * @Description 值域项列表
     * @Date 2019/5/10 8:51
     * @Param [request, map]
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/queryList",method = RequestMethod.POST)
    public ResponseEntity querySjy(HttpServletRequest request, @RequestBody Map map){
        PageBean page = (PageBean) ConverterUtils.map2ObjectWithoutNullValue((Map<String, String>) map.get("page"),PageBean.class);
        map.put("page",page);
        map.put("zdmNotNull",true);
        List list = zyglService.queryList(map);
        return PaginationResponseEntity.ok(page).put("rows",list);
    }

    /*
     * @Author tangzj
     * @Description 保存值域项
     * @Date 2019/5/10 8:54
     * @Param []
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/saveZyx",method = RequestMethod.POST)
    public ResponseEntity saveZyx(@RequestBody Zygl zygl){
        if (!Common.validateParams(zygl.getZdm())) {
            return ResponseEntity.error(201, "参数为空");
        }
        //值代码是否存在
        Map<String,Object> map =  new HashedMap<String,Object>();
        map.put("zdm",zygl.getZdm());
        Long i =  zyglService.queryByZdm(map);
        if(i>0){
            return ResponseEntity.error(201, "值代码已经存在");
        }
        Map<String,Object> mapQ =  new HashedMap<String,Object>();
        mapQ.put("zydm",zygl.getZydm());
        mapQ.put("zdmIsNull",true);
        List<Zygl> list = zyglService.queryList(mapQ);
        if(list.size()==1){
            //查询当前分类是否存在且有值代码为空的情况  如果存在就更新 否则插入
            Zygl zyglUpdate = list.get(0);
            this.CommonZy(zygl,zyglUpdate);
            this.zyglService.update(zygl);
        }else {
            Zygl zyglQ = new Zygl();
            zyglQ.setZydm(zygl.getZydm());
            zyglQ = this.zyglService.queryTop1(zyglQ);
            if (zygl==null){
                return ResponseEntity.error(201, "系统有误，请联系管理员");
            }
            this.CommonZy(zygl,zyglQ);
            this.zyglService.saveZyx(zygl);
        }
        return  ResponseEntity.ok();
    }

    /*
     * @Author tangzj
     * @Description 更新值域项
     * @Date 2019/5/10 16:28
     * @Param [zygl]
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/updateZyx",method = RequestMethod.POST)
    public ResponseEntity updateZyx(@RequestBody Zygl zygl){
        this.zyglService.updateZyx(zygl);
        return  ResponseEntity.ok();
    }

    /*
     * @Author tangzj
     * @Description 删除值域/值域项
     * @Date 2019/5/10 9:15
     * @Param [zydm]
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/delZyx",method = RequestMethod.GET)
    public ResponseEntity delZyx(@RequestParam String zdm, @RequestParam String zydm){
        if (!Common.validateParams(zdm) && !Common.validateParams(zydm)) {
            return ResponseEntity.error(201, "参数为空");
        }
        //先查询库中当前值代码下的值域还有几个，如果是一个的话，更新数据，否则删除
        Map<String,Object> map  = new HashedMap<String,Object>();
        map.put("zdm",zdm);
        map.put("zydm",zydm);
        List<Zygl> list = this.zyglService.queryList(map);
        if(list.size()!=1){
            this.zyglService.delete(map);
        }else{
            Zygl zygl = list.get(0);
            this.zyglService.delFromUpdate(zygl);
        }
        return  ResponseEntity.ok();
    }

    /*
     * @Author tangzj
     * @Description 获取值域项
     * @Date 2019/5/10 15:19
     * @Param [zdm]
     * @return com.winning.psms.common.util.ResponseEntity
     **/
    @RequestMapping(value="/getZyx",method = RequestMethod.GET)
    public ResponseEntity getZyx(@RequestParam String zdm, @RequestParam String zydm){
        if (!Common.validateParams(zdm)) {
            return ResponseEntity.error(201, "参数为空");
        }
        Map<String,Object> map  = new HashedMap<String,Object>();
        map.put("zdm",zdm);
        map.put("zydm",zydm);
        Zygl zygl =  this.getZygl(map);
        ResponseEntity responseEntity = new ResponseEntity();
        return  responseEntity.put("zyx",zygl);
    }
}
